import cv2
import numpy as np


def next_steering(centers, img_width, look_ahead=30, margin=0.15):
    """
    根据中心线坐标判断转向方向
    centers: (N,2) 已经放大20倍的中心线像素坐标
    img_w:   放大后图像总宽（=原宽*20）
    look_ahead: 往前看多少像素（行数）
    margin:  左右保留多少比例算“中心区”，默认 15%
    return:  'LEFT'/'RIGHT'/'STRAIGHT'
    """
    if centers.shape[0] < look_ahead:
        return 'STRAIGHT'          # 数据不够，保守直行

    # 只取前方 look_ahead 列
    future = centers[-look_ahead:, 0]
    x_mean = int(future.mean())

    left_bound  = int(img_width * margin)
    right_bound = int(img_width * (1 - margin))

    if x_mean < left_bound:
        return 'LEFT'
    elif x_mean > right_bound:
        return 'RIGHT'
    else:
        return 'STRAIGHT'

    
# # big 是已经放大20倍的图
# h_big, w_big = big.shape[:2]
# cmd = next_steering(centers, w_big)
# print('下一步指令:', cmd)

# # 可视化：把指令画在图上
# cv2.putText(big, cmd, (50, 100),
#             cv2.FONT_HERSHEY_SIMPLEX, 4, (0, 255, 0), 6)
# cv2.imshow('放大20倍+指令', big)
# cv2.waitKey(0)